<div id='output'>Loading...</div>
<script type='text/javascript'>
//This sample displays all available profile fields of the viewer.

// This array is what we send to the OpenSocial call specifying
// what are the fields we want
var fields = [];

// This array is for our own use when displaying the returned
// information. You don't need to have this in your applications --
// it's just a convenient way to organize this particular sample.
var fielddescs = [];

function addfield(name, key) {
   if (typeof key == 'undefined') {
      debug.say('!!! WARNING: field ' + name + ' is unknown.');
      return;
   }

   // add the field key to our fields[] array, which we will pass to the
   // OpenSocial call
   fields.push(key);

   // add a description of this field, with a name and a key, to our own
   // internal array which we will later use for displaying information.
   // This array is not passed to the OpenSocial call, it's just for our
   // own organization
   fielddescs.push( { name: name, key: key } );
}

function init() {
   addfield("ABOUT_ME", opensocial.Person.Field.ABOUT_ME);
   addfield("ADDRESSES", opensocial.Person.Field.ADDRESSES);
   addfield("BOOKS", opensocial.Person.Field.BOOKS);
   addfield("CURRENT_LOCATION", opensocial.Person.Field.CURRENT_LOCATION);
   addfield("FASHION", opensocial.Person.Field.FASHION);
   addfield("FOOD", opensocial.Person.Field.FOOD);
   addfield("GENDER", opensocial.Person.Field.GENDER);
   addfield("HUMOR", opensocial.Person.Field.HUMOR);
   addfield("INTERESTS", opensocial.Person.Field.INTERESTS);
   addfield("LANGUAGES_SPOKEN", opensocial.Person.Field.LANGUAGES_SPOKEN);
   addfield("LIVING_ARRANGEMENT", opensocial.Person.Field.LIVING_ARRANGEMENT);
   addfield("MOVIES", opensocial.Person.Field.MOVIES);
   addfield("MUSIC", opensocial.Person.Field.MUSIC);
   addfield("NAME", opensocial.Person.Field.NAME);
   addfield("PROFILE_URL", opensocial.Person.Field.PROFILE_URL);
   addfield("RELATIONSHIP_STATUS", opensocial.Person.Field.RELATIONSHIP_STATUS);
   addfield("SPORTS", opensocial.Person.Field.SPORTS);
   addfield("STATUS", opensocial.Person.Field.STATUS);
   addfield("THUMBNAIL_URL", opensocial.Person.Field.THUMBNAIL_URL);
   addfield("TURN_ONS", opensocial.Person.Field.TURN_ONS);
   addfield("TV_SHOWS", opensocial.Person.Field.TV_SHOWS);
   addfield("URLS", opensocial.Person.Field.URLS);

   req = opensocial.newDataRequest();
   params = [];
   params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = fields;
   req.add(req.newFetchPersonRequest(
           opensocial.IdSpec.PersonId.VIEWER, params), 'viewer');
   req.send(callback);
   debug.say("Request sent. Waiting for reply.");
}

function callback(resp) {
   if (resp.hadError()) {
      debug.say("*** Error ***");
      return;
   }
   debug.say("success.");
   var viewer = resp.get('viewer').getData();

   var html = [];

   // Each field of the viewer's profile is now available by calling
   //    viewer.getField(field)
   // Where field is a constant defined in opensocial.Person.Field.*
   // (for example, opensocial.Person.Field.TV_SHOWS)

   for (i in fielddescs) {
      var thisdesc = fielddescs[i];
      var fieldName = thisdesc.name;
      var fieldKey = thisdesc.key;
      var val = viewer.getField(fieldKey);
      var content;

      if (typeof val == 'undefined') content = "<em>undefined</em>";
      else if (val == null) content = "<em>null</em>";
      else
         content = gadgets.util.escapeString(gadgets.json.stringify(val));

      html.push("<p><b>" + fieldName + "</b>:<br />" + content + "</p>");
   }
   document.getElementById('output').innerHTML = html.join('');
}

gadgets.util.registerOnLoadHandler(init);
</script>

